// 定义一个正向字典
const dist = ['这','是','一个','简单','的', '中文','测试','分词','算法', '示例']
function isWord(str) {
  return dist.includes(str);
}

function segment(text) {
  var len = text.length;
  var maxLen = 4; // 给定最大分词长度
  var words = [];
  for (var i = 0; i < len; ) {
    var curMaxLen = Math.min(maxLen, len - i);
    var matched = false;
    for (var j = curMaxLen; j > 0; j--) {
      var token = text.substr(i, j);
      if (isWord(token)) {
        words.push(token);
        i += j;
        matched = true;
        break;
      }
    }
    if (!matched) {
      words.push(text.charAt(i));
      i++;
    }
  }
  return words;
}

console.log(segment('这是一个简单的中文分词算法示例'))